package com.hanlink.dsp.util;

import android.view.Gravity;

import com.hanlink.dsp.BtDspApplication;
import com.hanlink.dsp.DspContext;
import com.hanlink.dsp.DspSettings;

import com.hanlink.dsp.ca.model.channel.EqChannel;
import com.hanlink.dsp.ca.model.channel.LevelChannel;
import com.hanlink.dsp.ca.model.channel.SummingChannel;
import com.hanlink.dsp.ca.model.channel.XoverChannel;
import com.hanlink.dsp.ca.model.settings.AdvanceSettings;
import com.hanlink.dsp.ca.model.settings.EqSettings;
import com.hanlink.dsp.ca.model.settings.HomeSettings;
import com.hanlink.dsp.ca.model.settings.SummingSettings;
import com.hanlink.dsp.ca.model.settings.XoverSettings;
import com.hanlink.dsp.model.BaseSettings;
import com.hanlink.dsp.model.DspFile;
import com.hanlink.dsp.model.UserProfile;
import com.hanlink.dsp.protocols.XoverProtocol;
import com.qztech.sdk.model.SettingConstants;
import com.qztech.utils.YzDebug;

import org.xutils.DbManager;
import org.xutils.ex.DbException;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import static com.hanlink.dsp.util.Constants.MAX_VOL;

public class DataUtil {
    public static final String KEY_CHANNEL_NAME = "name_ch_";
    public static final String KEY_INPUT = "input_ch_";

    public static void resetDefaultAppData() {
        loadDefaultData();//加载数据，  主要是ID
        resetAppData();
    }

    public static void resetAppData() {
        try {
           /* YzDebug.d("resetAppData: ");
            if (DspSettings.HOME_SETTINGS == null) {
                DspSettings.HOME_SETTINGS = new HomeSettings();
            } else {
                DspSettings.HOME_SETTINGS.resetData();
            }
            if (DspSettings.EQ_SETTINGS == null) {
                DspSettings.EQ_SETTINGS = new EqSettings();
                //设置channel数值
                DspSettings.EQ_SETTINGS.getChannels(null);
            } else {
                DspSettings.EQ_SETTINGS.resetData();
            }
            if (DspSettings.XOVER_SETTINGS == null) {
                DspSettings.XOVER_SETTINGS = new XoverSettings();
                //XOVER 数据读取
                DspSettings.XOVER_SETTINGS.getChannels(null);
            } else {
                DspSettings.XOVER_SETTINGS.resetData();
            }

            if (DspSettings.ADVANCE_SETTINGS == null) {
                DspSettings.ADVANCE_SETTINGS = new AdvanceSettings();
                //读取Advance 配置
                DspSettings.ADVANCE_SETTINGS.getChannels(null);
            } else {
                DspSettings.ADVANCE_SETTINGS.resetData();
            }

            if (DspSettings.SUMMING_SETTINGS == null) {
                DspSettings.SUMMING_SETTINGS = new SummingSettings();
                DspSettings.SUMMING_SETTINGS.getChannels(null);
            } else {
                DspSettings.SUMMING_SETTINGS.resetData();
            }*/
            YzDebug.d("resetAppData: ");
            DspSettings.HOME_SETTINGS.resetData();
            DspSettings.EQ_SETTINGS.resetData();
            DspSettings.XOVER_SETTINGS.resetData();

            DspSettings.ADVANCE_SETTINGS.resetData();

            DspSettings.SUMMING_SETTINGS.resetData();
        } catch (Exception e) {
            YzDebug.d(e);
        }
    }



    public static void initDefaultUserProfile() {
        synchronized (DataUtil.class){
            DbManager dbManager = BtDspApplication.getInstance().getDbManager();
            try {
                UserProfile def = dbManager.selector(UserProfile.class).where(UserProfile.NAME, "=", DspSettings.DEFAULT_USERP_PROFILE.getName()).findFirst();
                YzDebug.d("+++++++++++++++++++++++1loadData: " + def);
                boolean isFirst = false;
                if (def == null) {
//                dbManager.saveOrUpdate(DspSettings.DEFAULT_USERP_PROFILE);
                    dbManager.save(DspSettings.DEFAULT_USERP_PROFILE);
                    isFirst = true;
                }
                if (def == null) {
                    def = dbManager.selector(UserProfile.class).where(UserProfile.NAME, "=", DspSettings.DEFAULT_USERP_PROFILE.getName()).findFirst();
                }
                YzDebug.d("+++++++++++++++++++++++2loadData: " + def);
                if (def != null) {
                    DspSettings.DEFAULT_USERP_PROFILE.setId(def.getId());
                    DspSettings.setUserProfile(def);
                    DspSettings.HOME_SETTINGS.setRefId(def.getId());
                    DspSettings.SUMMING_SETTINGS.setRefId(def.getId());
                    DspSettings.EQ_SETTINGS.setRefId(def.getId());
                    DspSettings.XOVER_SETTINGS.setRefId(def.getId());
                    DspSettings.ADVANCE_SETTINGS.setRefId(def.getId());
                    DspSettings.PLAY_SETTINGS.setRefId(def.getId());
                    if (isFirst){
                        saveEqToDb(def.getId(),dbManager,
                                DspSettings.HOME_SETTINGS,
                                DspSettings.SUMMING_SETTINGS,
                                DspSettings.EQ_SETTINGS,
                                DspSettings.XOVER_SETTINGS,
                                DspSettings.ADVANCE_SETTINGS
                        );
                    }
                }

            } catch (Exception e) {
                YzDebug.d("Error:initDefaultUserProfile", e);
            }
        }
        loadDefaultData();
    }
    public static void loadDefaultData() {
        try {
            int userProfileId = DspSettings.DEFAULT_USERP_PROFILE.getId();
            YzDebug.d("------loadDefaultData start: " );
            DbManager dbManager = BtDspApplication.getInstance().getDbManager();
            //EqSettings
            EqSettings eqSettings = dbManager.selector(EqSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (eqSettings == null) {
                eqSettings = new EqSettings();
            }

            //加载XOVER配置
            XoverSettings xoverSettings = dbManager.selector(XoverSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (xoverSettings == null) {
                xoverSettings = new XoverSettings();
            }


            //AdvanceSettings
            AdvanceSettings advanceSettings = dbManager.selector(AdvanceSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (advanceSettings == null) {
                advanceSettings = new AdvanceSettings();
            }

            //SummingSettings
            SummingSettings summingSettings = dbManager.selector(SummingSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (summingSettings == null) {
                summingSettings = new SummingSettings();
            }
            eqSettings.getChannels(dbManager);
            xoverSettings.getChannels(dbManager);
            advanceSettings.getChannels(dbManager);
            summingSettings.getChannels(dbManager);

            DspSettings.EQ_SETTINGS = eqSettings;
            DspSettings.XOVER_SETTINGS = xoverSettings;
            DspSettings.ADVANCE_SETTINGS = advanceSettings;
            DspSettings.SUMMING_SETTINGS = summingSettings;
        } catch (Exception e) {
            YzDebug.d("Error:loadDataByUserProfileId", e);
        }
    }
    private static void loadDataByUserProfileId(int userProfileId) {
        try {
            YzDebug.d("------loadDataByUserProfileId start: " + userProfileId);
            DbManager dbManager = BtDspApplication.getInstance().getDbManager();
            //EqSettings
            EqSettings eqSettings = dbManager.selector(EqSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (eqSettings == null) {
                eqSettings = new EqSettings();
            }

            //加载XOVER配置
            XoverSettings xoverSettings = dbManager.selector(XoverSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (xoverSettings == null) {
                xoverSettings = new XoverSettings();
            }


            //AdvanceSettings
            AdvanceSettings advanceSettings = dbManager.selector(AdvanceSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (advanceSettings == null) {
                advanceSettings = new AdvanceSettings();
            }

            //SummingSettings
            SummingSettings summingSettings = dbManager.selector(SummingSettings.class).where(BaseSettings.REF_ID, "=", userProfileId).findFirst();
            if (summingSettings == null) {
                summingSettings = new SummingSettings();
            }
            eqSettings.getChannels(dbManager);
            xoverSettings.getChannels(dbManager);
            advanceSettings.getChannels(dbManager);
            summingSettings.getChannels(dbManager);

            eqSettings.copyIdFromSrc(DspSettings.EQ_SETTINGS);
            xoverSettings.copyIdFromSrc(DspSettings.XOVER_SETTINGS);
            advanceSettings.copyIdFromSrc(DspSettings.ADVANCE_SETTINGS);
            summingSettings.copyIdFromSrc(DspSettings.SUMMING_SETTINGS);

            DspSettings.EQ_SETTINGS = eqSettings;
            DspSettings.XOVER_SETTINGS = xoverSettings;
            DspSettings.ADVANCE_SETTINGS = advanceSettings;
            DspSettings.SUMMING_SETTINGS = summingSettings;
            //设置channel数值
            YzDebug.d("------loadDataByUserProfileId end: " + userProfileId);
        } catch (Exception e) {
            YzDebug.d("Error:loadDataByUserProfileId", e);
        }
    }

    public static void loadData(int userProfileId) {
        try {
            initDefaultUserProfile();
            loadDataByUserProfileId(userProfileId);
        } catch (Exception ex) {
            ex.printStackTrace();
            YzDebug.d("loaddata", ex);
        }
    }


    public static void loadData() {
        loadData(DspSettings.getUserProfile().getId());
    }



    public static boolean saveDspToShareFile(File file, UserProfile userProfile) {
        if (userProfile == null || userProfile.getId() <= 0) return false;
        FileWriter fileWriter = null;
        try {
            fileWriter = new FileWriter(file);

            int objId = userProfile.getId();
            DbManager dbManager = BtDspApplication.getInstance().getDbManager();

            //eq
            EqSettings eqSettings = dbManager.selector(EqSettings.class).where(BaseSettings.REF_ID, "=", objId).findFirst();
            if (eqSettings != null) {
                eqSettings.getChannels(dbManager);
                List<EqChannel> eqChannels = eqSettings.getEqChannels();
                for (int i = 0; i < eqChannels.size(); i++) {
                    EqChannel tmpCh = eqChannels.get(i);
                    for (int index = 0, len = DspSettings.FRENCY_LENGTH; index < len; index++) {
                        fileWriter.write(ByteUtil.bytesToHexString(tmpCh.getCmd(false, index)) + "\n");
                    }
                }
            }

            //Xover
            XoverSettings xoverSettings = dbManager.selector(XoverSettings.class).where(BaseSettings.REF_ID, "=", objId).findFirst();
            if (xoverSettings != null) {
                xoverSettings.getChannels(dbManager);
                List<XoverChannel> xoverChannels = xoverSettings.getXoverChannels();
                for (int i = 0, len = xoverChannels.size(); i < len; i++) {
                    XoverChannel xoverChannel = xoverChannels.get(i);
                    int index = xoverChannel.getChannel();
                    byte channel = (byte) (0xC1 + index);
                    fileWriter.write(ByteUtil.bytesToHexString(XoverProtocol.getCmd(xoverChannel, xoverChannel.getPassType() == 3 ? 3 : 1, channel)) + "\n");
                    fileWriter.write(ByteUtil.bytesToHexString(XoverProtocol.getCmd(xoverChannel, xoverChannel.getPassType() == 4 ? 4 : 2, channel)) + "\n");
                }
            }

            //Advance
            AdvanceSettings advanceSettings = dbManager.selector(AdvanceSettings.class).where(BaseSettings.REF_ID, "=", objId).findFirst();
            if (advanceSettings != null) {
                advanceSettings.getChannels(dbManager);
                List<LevelChannel> levelChannels = advanceSettings.getChannel();
                for (int i = 0; i < levelChannels.size(); i++) {
                    LevelChannel levelChannel = levelChannels.get(i);
                    fileWriter.write(ByteUtil.bytesToHexString(levelChannel.getVolumeBuffer()) + "\n");
                    fileWriter.write(ByteUtil.bytesToHexString(levelChannel.getDegreeBuffer()) + "\n");
                }
                for (int index = 0; index < DspSettings.CHANNEL_LENGTH; index++) {
                    fileWriter.write(ByteUtil.bytesToHexString(advanceSettings.getTimeDelayBuffer(index)) + "\n");
                }
                fileWriter.write(ByteUtil.bytesToHexString(advanceSettings.getTotalVolBuffer(advanceSettings.getTotalVol(), advanceSettings.getMute() == 1)) + "\n");
            }

            //Summing
            SummingSettings summingSettings = dbManager.selector(SummingSettings.class).where(BaseSettings.REF_ID, "=", objId).findFirst();
            if (summingSettings != null) {
                summingSettings.getChannels(dbManager);
                List<SummingChannel> summingChannels = summingSettings.getSummingChannels();
                for (SummingChannel summingChannel : summingChannels) {
                    fileWriter.write(ByteUtil.bytesToHexString(summingChannel.getCmd(true)) + "\n");
                }
            }
            fileWriter.flush();
            fileWriter.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    private static void _saveSummingSettingsToDb(DbManager db) {
        try {
            SummingSettings summingSettings = DspSettings.SUMMING_SETTINGS;
            //  summingSettings.setId(0);
            YzDebug.d("_saveSummingSettingsToDb before: " + summingSettings.getId());
            summingSettings.setRefId(DspSettings.getUserProfile().getId());
//            db.delete(SummingSettings.class, WhereBuilder.b(SummingSettings.REF_ID, "=", DspSettings.getUserProfile().getId()));
            db.saveOrUpdate(summingSettings);
            YzDebug.d("_saveSummingSettingsToDb: " + summingSettings.getId() + " " + summingSettings.getRefId());
            int summing_setting_id = summingSettings.getId();
            if (summing_setting_id > 0) {
                if (summingSettings.getSummingChannels() != null && summingSettings.getSummingChannels().size() > 0) {
                    for (SummingChannel summingChannel : summingSettings.getSummingChannels()) {
                        //  summingChannel.setId(0);
                        summingChannel.setSettingsId(summing_setting_id);
                    }
                    db.saveOrUpdate(summingSettings.getSummingChannels());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveSummingSettingsToDb() {
        _saveSummingSettingsToDb(BtDspApplication.getInstance().getDbManager());
    }

    private static void _saveEqSettingsToDb(DbManager db) {

        try {
            EqSettings eqSettings = DspSettings.EQ_SETTINGS;
            // eqSettings.setId(0);
            eqSettings.setRefId(DspSettings.getUserProfile().getId());
            YzDebug.d("_saveEqSettingsToDb   id:"+DspSettings.getUserProfile().getId());
            db.saveOrUpdate(eqSettings);

            int eqSettingsId = eqSettings.getId();
            if (eqSettingsId > 0) {
                if (eqSettings.getEqChannels() != null && eqSettings.getEqChannels().size() > 0) {
                    for (EqChannel eqChannel : eqSettings.getEqChannels()) {
                        // eqChannel.setId(0);
                        eqChannel.setSettingsId(eqSettingsId);
                    }
                    List<EqChannel> ecs = eqSettings.getEqChannels();
                    int i = 0;
                    for (EqChannel ec : ecs) {
                        YzDebug.d("_saveEqSettingsToDb " + i + " -:" + ec.getId() + " " + ec.getSettingsId());
                        YzDebug.d("_saveEqSettingsToDb " + i + " f:" + ec.getFreqVal());
                        YzDebug.d("_saveEqSettingsToDb " + i + " q:" + ec.getQVal());
                        YzDebug.d("_saveEqSettingsToDb " + i + " g:" + ec.getGainVal());
                        i++;
                    }

                    db.saveOrUpdate(eqSettings.getEqChannels());
                }
            }
        } catch (Exception e) {
            YzDebug.d("_saveEqSettingsToDb", e);
        }
    }

    public static void saveEqSettingsToDb() {
        _saveEqSettingsToDb(BtDspApplication.getInstance().getDbManager());
    }

    private static void _saveXoverSettingsToDb(DbManager db) {
        YzDebug.d("_saveXoverSettingsToDb");
        try {
            //保存XOVER 数据
            XoverSettings xoverSettings = DspSettings.XOVER_SETTINGS;
            xoverSettings.setRefId(DspSettings.getUserProfile().getId());
            db.saveOrUpdate(xoverSettings);

            int xOverSettingsId = xoverSettings.getId();
            if (xOverSettingsId > 0) {
                if (xoverSettings.getXoverChannels() != null && xoverSettings.getXoverChannels().size() > 0) {
                    for (XoverChannel channel : xoverSettings.getXoverChannels()) {
                        //channel.setId(0);
                        channel.setSettingsId(xOverSettingsId);
                    }

                    db.saveOrUpdate(xoverSettings.getXoverChannels());
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveXoverSettingsToDb() {
        _saveXoverSettingsToDb(BtDspApplication.getInstance().getDbManager());
    }

    private static void _saveAdvanceSettingsToDb(DbManager db) {
        YzDebug.d("_saveAdvanceSettingsToDb");
        try {
            AdvanceSettings advanceSettings = DspSettings.ADVANCE_SETTINGS;
            // advanceSettings.setId(0);
            advanceSettings.setRefId(DspSettings.getUserProfile().getId());
            db.saveOrUpdate(advanceSettings);

            int advanceSettingsId = advanceSettings.getId();
            // if (advanceSettingsId > 0)
            {
                if (advanceSettings.getChannel() != null
                        && advanceSettings.getChannel().size() > 0) {
                    for (LevelChannel channel : advanceSettings.getChannel()) {
                        //channel.setId(0);
                        channel.setSettingsId(advanceSettingsId);
                    }
                    db.saveOrUpdate(advanceSettings.getChannel());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void saveAdvanceSettingsToDb() {
        _saveAdvanceSettingsToDb(BtDspApplication.getInstance().getDbManager());
    }

    public static boolean saveDspFileToDb() {
        try {
            DbManager db = BtDspApplication.getInstance().getDbManager();
            UserProfile userProfile = DspSettings.getUserProfile();
            db.saveOrUpdate(userProfile);
            int userProfileId = userProfile.getId();
            if (userProfileId > 0) {
                SummingSettings summingSettings = DspSettings.SUMMING_SETTINGS;
                //   summingSettings.setId(0);
                summingSettings.setRefId(userProfileId);
                db.saveOrUpdate(summingSettings);
                int summing_setting_id = summingSettings.getId();
                if (summing_setting_id > 0) {
                    if (summingSettings.getSummingChannels() != null && summingSettings.getSummingChannels().size() > 0) {
                        for (SummingChannel summingChannel : summingSettings.getSummingChannels()) {
                            //summingChannel.setId(0);
                            summingChannel.setSettingsId(summing_setting_id);
                        }
                        db.saveOrUpdate(summingSettings.getSummingChannels());
                    }
                }

                //保存EQ EqChannel 数据
                EqSettings eqSettings = DspSettings.EQ_SETTINGS;
                // eqSettings.setId(0);
                eqSettings.setRefId(userProfileId);
                db.saveOrUpdate(eqSettings);

                int eqSettingsId = eqSettings.getId();
                if (eqSettingsId > 0) {
                    if (eqSettings.getEqChannels() != null && eqSettings.getEqChannels().size() > 0) {
                        for (EqChannel eqChannel : eqSettings.getEqChannels()) {
                            // eqChannel.setId(0);
                            eqChannel.setSettingsId(eqSettingsId);
                        }
                        db.saveOrUpdate(eqSettings.getEqChannels());
                    }
                }


                //保存XOVER 数据
                XoverSettings xoverSettings = DspSettings.XOVER_SETTINGS;
                // xoverSettings.setId(0);
                xoverSettings.setRefId(userProfileId);
                db.saveOrUpdate(xoverSettings);

                int xOverSettingsId = xoverSettings.getId();
                if (xOverSettingsId > 0) {
                    if (xoverSettings.getXoverChannels() != null && xoverSettings.getXoverChannels().size() > 0) {
                        for (XoverChannel channel : xoverSettings.getXoverChannels()) {
                            //   channel.setId(0);
                            channel.setSettingsId(xOverSettingsId);
                        }

                        db.saveOrUpdate(xoverSettings.getXoverChannels());
                    }
                }


                //保存ADVANCE 数据
                AdvanceSettings advanceSettings = DspSettings.ADVANCE_SETTINGS;
                //  advanceSettings.setId(0);
                advanceSettings.setRefId(userProfileId);
                db.saveOrUpdate(advanceSettings);

                int advanceSettingsId = advanceSettings.getId();
                if (advanceSettingsId > 0) {
                    if (advanceSettings.getChannel() != null && advanceSettings.getChannel().size() > 0) {
                        for (LevelChannel channel : advanceSettings.getChannel()) {
                            // channel.setId(0);
                            channel.setSettingsId(advanceSettingsId);
                        }
                        db.saveOrUpdate(advanceSettings.getChannel());
                    }
                }
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean saveDspFileToDb(DbManager db, DspFile dspFile) {
        try {
            UserProfile userProfile = dspFile.getUserProfile();
            userProfile.setId(0);
            db.saveOrUpdate(dspFile.getUserProfile());
            int userProfileId = userProfile.getId();
            if (userProfileId > 0) {
                SummingSettings summingSettings = dspFile.getSummingSettings();
                summingSettings.setId(0);
                summingSettings.setRefId(userProfileId);
                db.saveOrUpdate(summingSettings);
                int summing_setting_id = summingSettings.getId();
                if (summing_setting_id > 0) {
                    if (summingSettings.getSummingChannels() != null && summingSettings.getSummingChannels().size() > 0) {
                        for (SummingChannel summingChannel : summingSettings.getSummingChannels()) {
                            summingChannel.setId(0);
                            summingChannel.setSettingsId(summing_setting_id);
                        }
                        db.saveOrUpdate(summingSettings.getSummingChannels());
                    }
                }

                //保存EQ EqChannel 数据
                EqSettings eqSettings = dspFile.getEqSettings();
                eqSettings.setId(0);
                eqSettings.setRefId(userProfileId);
                db.saveOrUpdate(eqSettings);

                int eqSettingsId = eqSettings.getId();
                if (eqSettingsId > 0) {
                    if (eqSettings.getEqChannels() != null && eqSettings.getEqChannels().size() > 0) {
                        for (EqChannel eqChannel : eqSettings.getEqChannels()) {
                            eqChannel.setId(0);
                            eqChannel.setSettingsId(eqSettingsId);
                        }
                        db.saveOrUpdate(eqSettings.getEqChannels());
                    }
                }


                //保存XOVER 数据
                XoverSettings xoverSettings = dspFile.getXoverSettings();
                xoverSettings.setId(0);
                xoverSettings.setRefId(userProfileId);
                db.saveOrUpdate(xoverSettings);

                int xOverSettingsId = xoverSettings.getId();
                if (xOverSettingsId > 0) {
                    if (xoverSettings.getXoverChannels() != null && xoverSettings.getXoverChannels().size() > 0) {
                        for (XoverChannel channel : xoverSettings.getXoverChannels()) {
                            channel.setId(0);
                            channel.setSettingsId(xOverSettingsId);
                        }

                        db.saveOrUpdate(xoverSettings.getXoverChannels());
                    }
                }


                //保存ADVANCE 数据
                AdvanceSettings advanceSettings = dspFile.getAdvanceSettings();
                advanceSettings.setId(0);
                advanceSettings.setRefId(userProfileId);
                db.saveOrUpdate(advanceSettings);

                int advanceSettingsId = advanceSettings.getId();
                if (advanceSettingsId > 0) {
                    if (advanceSettings.getChannel() != null && advanceSettings.getChannel().size() > 0) {
                        for (LevelChannel channel : advanceSettings.getChannel()) {
                            channel.setId(0);
                            channel.setSettingsId(advanceSettingsId);
                        }
                        db.saveOrUpdate(advanceSettings.getChannel());
                    }
                }
                return true;
            }
            return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void setupPreEffetData(int index) {
        DspSettings.updateUserProfile(DspSettings.DEFAULT_USERP_PROFILE);
        DataUtil.resetDefaultAppData();
        switch (index) {
            case 0:
            case 2:
            case 5:
                DspSettings.SUMMING_SETTINGS.setAssign(1);
                break;
            case 1:
            case 3:
            case 4:
                DspSettings.SUMMING_SETTINGS.setAssign(2);
                break;
        }
        //SUMMING_SETTINGS  reset


//        DspSettings.EQ_SETTINGS = new EqSettings();
//        List<EqChannel> eqChannels = new ArrayList<>(DspSettings.CHANNEL_LENGTH);
        for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
            EqChannel eqChannel = DspSettings.EQ_SETTINGS.getEqChannels().get(i);
            eqChannel.setFreq(new int[]{20, 25, 31, 40, 50, 63, 80, 100, 120, 160, 200, 250, 315, 400, 500, 630, 800, 1000, 1250, 1600, 2000, 2500, 3150, 4000, 5000, 6300, 8000, 10000, 12500, 16000, 20000});
            float[] q = new float[DspSettings.FRENCY_LENGTH];
            Arrays.fill(q, 400);
            eqChannel.setQ(q);
            int tmpGain[] = null;
            switch (index) {
                case 0:
                    tmpGain = new int[]{-1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 2, 2, 1, 0, 0, 0, 0, -1, -1, -1, -1};
                    break;
                case 1:
                    tmpGain = new int[]{8, 8, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 1, 2, 3, 3, 3, 4, 5, 6, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0};
                    break;
                case 2:
                    tmpGain = new int[]{5, 6, 7, 5, 3, 1, 3, 4, 4, 6, 4, 2, 0, -1, -3, 0, 1, 2, 3, 4, 5, 6, 7, 5, 4, 6, 7, 6, 5, 4, 4};
                    break;
                case 3:
                    tmpGain = new int[]{6, 5, 5, 4, 3, 2, 1, 1, 0, 0, 0, 1, 1, 0, -1, 0, 0, 0, 0, 1, 2, 3, 4, 3, 3, 3, 3, 3, 3, 3, 3};
                    break;
                case 4:
                    tmpGain = new int[]{-4, -4, -4, -4, -4, -4, -3, -2, -2, -2, -1, 2, 3, 4, 4, 6, 7, 9, 9, 7, 5, 4, 2, 1, 0, -1, -2, -2, -3, -3, -3};
                    break;
                case 5:
                    tmpGain = new int[]{7, 6, 5, 4, 5, 6, 7, 8, 9, 9, 8, 6, 4, 2, 0, 0, -1, -2, -3, -4, -2, -1, 0, 1, 2, 3, 2, 1, 0, -1, -2};
                    break;
            }
            if (tmpGain == null)return;
            for(int k = 0, len = tmpGain.length; k < len; k++){
                tmpGain[k] = tmpGain[k]*10 + 200;
            }
            eqChannel.setGain(tmpGain);
        }

        //Xover
//        DspSettings.XOVER_SETTINGS = new XoverSettings();
//        List<XoverChannel> xoverChannels = new ArrayList<>(DspSettings.CHANNEL_LENGTH);
        int freq1 = 110;
        int freq2 = 90;
        int flitervale1 = 3;
        int flitervale2 = 3;
        switch (index) {
            case 0:
            case 1:
            case 4:
                freq1 = 110;
                freq2 = 90;
                break;
            case 2:
            case 3:
                flitervale1 = 1;
                flitervale2 = 1;
                break;
            case 5:
                freq2 = 110;
                break;
        }
        for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
            XoverChannel xoverChannel = DspSettings.XOVER_SETTINGS.getXoverChannels().get(i);
            if (i == 0 || i == 1) {
                xoverChannel.setHighFrequency(freq1);
                xoverChannel.setHighFilterType(1);
                xoverChannel.setHighFilterValue(flitervale1);
                xoverChannel.setPassType(1);
            } else if (i == 2 || i == 3) {
                xoverChannel.setHighFrequency(freq2);
                xoverChannel.setHighFilterType(1);
                xoverChannel.setHighFilterValue(flitervale2);
                xoverChannel.setPassType(1);
            }
//            xoverChannels.add(xoverChannel);
        }
//        DspSettings.XOVER_SETTINGS.setChannel(xoverChannels);


        //Advance
//        DspSettings.ADVANCE_SETTINGS = new AdvanceSettings();
//        List<LevelChannel> levelChannels = new ArrayList<>(DspSettings.CHANNEL_LENGTH);
        for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
            LevelChannel levelChannel = DspSettings.ADVANCE_SETTINGS.getChannelByIndex(i);
            levelChannel.setVal(MAX_VOL - 6);
//            levelChannels.add(levelChannel);
        }
        DspSettings.ADVANCE_SETTINGS.setTotalVol(MAX_VOL - 6);
//        DspSettings.ADVANCE_SETTINGS.setChannel(levelChannels);
        DspSettings.ADVANCE_SETTINGS.setChannelChecked(0, 1, true);
        //EQ
        /*DspSettings.EQ_SETTINGS = new EqSettings();
        List<EqChannel> eqChannels = new ArrayList<>(DspSettings.CHANNEL_LENGTH);
        for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
            EqChannel eqChannel = null;
            switch (index) {
                case 0:
                    eqChannel = new EqChannel(i,
                            new float[]{100f, 180f, 50f, 5f, 200f, 100f, 350f, 200f},
                            new int[]{0, 320, 0, 200, 235, 230, 170, 165},
                            new int[]{30, 60, 90, 315, 1250, 3200, 6000, 16000}
                    );
                    break;
                case 1:
                    eqChannel = new EqChannel(i,
                            new float[]{100f, 180f, 50f, 5f, 200f, 100f, 350f, 200f},
                            new int[]{0, 320, 0, 200, 235, 230, 140, 165},
                            new int[]{30, 60, 90, 315, 1250, 3200, 8000, 16000}
                    );
                    break;
                case 2:
                    eqChannel = new EqChannel(i,
                            new float[]{50f, 180f, 50f, 5f, 430f, 5f, 430f, 160f},//q
                            new int[]{0, 260, 115, 200, 125, 200, 168, 88},//gain
                            new int[]{80, 160, 500, 750, 2500, 3200, 6300, 10000}//freq
                    );
                    break;
                case 3:
                    eqChannel = new EqChannel(i,
                            new float[]{600f, 50f, 180f, 200f, 430f, 5f, 430f, 160f},
                            new int[]{260, 0, 260, 115, 125, 200, 108, 88},
                            new int[]{60, 80, 160, 500, 2500, 3200, 6300, 10000}
                    );
                    break;
                case 4:
                    eqChannel = new EqChannel(i,
                            new float[]{500f, 5f, 500f, 5f, 200f, 400f, 500f, 700f},
                            new int[]{260, 200, 170, 200, 170, 230, 160, 180},
                            new int[]{50, 80, 200, 500, 2000, 5000, 8000, 16000}
                    );
                    break;
                case 5:
                    eqChannel = new EqChannel(i,
                            new float[]{100f, 400f, 5f, 5f, 200f, 500f, 1000f, 5f},
                            new int[]{260, 180, 200, 200, 140, 110, 110, 200},
                            new int[]{50, 100, 200, 500, 2000, 6000, 16000, 20000}
                    );
                    break;
            }
            eqChannels.add(eqChannel);
        }
        DspSettings.EQ_SETTINGS.setLastChannel(0);
        DspSettings.EQ_SETTINGS.setEqChannel(eqChannels);

        //Xover
        DspSettings.XOVER_SETTINGS = new XoverSettings();
        List<XoverChannel> xoverChannels = new ArrayList<>(DspSettings.CHANNEL_LENGTH);
        int freq1 = 90;
        int freq2 = 60;
        switch (index) {
            case 0:
            case 1:
                freq1 = 80;
                freq2 = 70;
                break;
        }
        for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
            XoverChannel xoverChannel = new XoverChannel(i);
            if (i == 0 || i == 1) {
                xoverChannel.setHighFrequency(freq1);
                xoverChannel.setHighFilterType(1);
                xoverChannel.setHighFilterValue(1);
                xoverChannel.setPassType(1);
            } else if (i == 2 || i == 3) {
                xoverChannel.setHighFrequency(freq2);
                xoverChannel.setHighFilterType(1);
                xoverChannel.setHighFilterValue(1);
                xoverChannel.setPassType(1);
            }
            xoverChannels.add(xoverChannel);
        }
        DspSettings.XOVER_SETTINGS.setChannel(xoverChannels);
        //Advance
        DspSettings.ADVANCE_SETTINGS = new AdvanceSettings();
        List<LevelChannel> levelChannels = new ArrayList<>(DspSettings.CHANNEL_LENGTH);
        for (int i = 0; i < DspSettings.CHANNEL_LENGTH; i++) {
            LevelChannel levelChannel = new LevelChannel(i);
            levelChannel.setVal(44);
            levelChannels.add(levelChannel);
        }
        DspSettings.ADVANCE_SETTINGS.setTotalVol(44);
        DspSettings.ADVANCE_SETTINGS.setChannel(levelChannels);*/
    }


    public static void saveEqToDb(int profileId, DbManager db,
                                  HomeSettings homeSettings,
                                  SummingSettings summingSettings,
                                  EqSettings eqSettings,
                                  XoverSettings xoverSettings,
                                  AdvanceSettings advanceSettings
                                  ){
        try {


            YzDebug.d("saveEqToDb>>>>>>>>>>>>>>>>>>>>>>>>>>:" + profileId);

            if (profileId > 0) {
                final HomeSettings hs = homeSettings.copy();
                hs.setId(0);
                hs.setRefId(profileId);
                db.saveOrUpdate(hs);
//========================
                final SummingSettings ss = summingSettings.copy();
                ss.setId(0);
                ss.setRefId(profileId);
                db.saveOrUpdate(ss);
                int summing_setting_id = ss.getId();
                if (summing_setting_id > 0) {

                    for (SummingChannel summingChannel : ss.getSummingChannels()) {
                        summingChannel.setId(0);
                        summingChannel.setSettingsId(summing_setting_id);

                    }
                    db.saveOrUpdate(ss.getSummingChannels());
                    int index = 0;
                    for (SummingChannel summingChannel : ss.getSummingChannels()) {
                        YzDebug.d("saveEqToDb SUMMING_SETTINGS:" + index + " id:" + summingChannel.getId() + " summingid:" + summing_setting_id + " " + summingChannel.getHighInputVal());

                        index++;
                    }
                }

                //保存EQ EqChannel 数据
                final EqSettings es =  eqSettings.copy();
                es.setId(0);
                es.setRefId(profileId);
                db.saveOrUpdate(es);

                int eqSettingsId = es.getId();
                YzDebug.d(":::::::::::saveEqToDb eqSettingsId:" + eqSettingsId);
                if (eqSettingsId > 0) {
                    for (EqChannel eqChannel : es.getEqChannels()) {
                        eqChannel.setId(0);
                        eqChannel.setSettingsId(eqSettingsId);
                    }
                    db.saveOrUpdate(es.getEqChannels());
                }


                //保存XOVER 数据
                final XoverSettings xs =  xoverSettings.copy();
                xs.setId(0);
                xs.setRefId(profileId);
                db.saveOrUpdate(xs);

                int xOverSettingsId = xs.getId();
                YzDebug.d("saveEqToDb XOVER_SETTINGS id:"+ xOverSettingsId);
                if (xOverSettingsId > 0) {
                    for (XoverChannel channel : xs.getXoverChannels()) {
                        channel.setId(0);
                        channel.setSettingsId(xOverSettingsId);
                    }
                    db.saveOrUpdate(xs.getXoverChannels());
                    for (XoverChannel channel : xs.getXoverChannels()) {
                        YzDebug.d("--saveEqToDb xover:" + channel);
                    }
                }


                //保存ADVANCE 数据
                final AdvanceSettings as = advanceSettings.copy();
                as.setId(0);
                as.setRefId(profileId);
                db.saveOrUpdate(as);

                int advanceSettingsId = as.getId();
                YzDebug.d("saveEqToDb ADVANCE_SETTINGS id:"+ advanceSettingsId);
                if (advanceSettingsId > 0) {
                    for (LevelChannel channel : as.getChannel()) {
                        channel.setId(0);
                        channel.setSettingsId(advanceSettingsId);
                    }
                    db.saveOrUpdate(as.getChannel());
                }
            }
        } catch (DbException e) {
            e.printStackTrace();
            YzDebug.d("saveEqToDb",e);
        }
    }
}